package myicesscheduler.web;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyFileCompressorTest
{
   @BeforeClass
   static public void beforeClass()
   {
      final String delim = String.valueOf( File.separatorChar );

      System.setProperty( MyFolderUtil.uploadFolder_folderPropertyName, "." + delim + "WebContent"
            + delim + "WEB-INF" + delim + "upload" );

      System.setProperty( MyFolderUtil.backupFolder_folderPropertyName, "." + delim + "WebContent"
            + delim + "WEB-INF" + delim + "backup" );
   }

   private String parentFolderOfUpload;

   @Before
   public void before()
   {
      parentFolderOfUpload = new File( MyFolderUtil.uploadFolder.getAbsolutePath() ).getParent();
   }

   @Test
   public void CompressOneFile() throws IOException
   {
      final String dummyFile = MyFolderUtil.uploadFolder.getAbsolutePathOfFile( "dummyForTest.txt" );
      final String[] dummyFiles = new String[] { dummyFile };

      writeDummyData( dummyFile, 20 );

      final String zipPath = MyFolderUtil.backupFolder.getAbsolutePathOfFile( "testOne" );
      final String[] zipFilenames = MyFileCompressor.compressFiles( zipPath, parentFolderOfUpload,
            dummyFiles, 30,
            MyFileCompressor.CompressedFilenamePostfixType.THREE_DIGIT_START_FROM_ZERO );

      assertEquals( 1, zipFilenames.length );
   }

   @Test
   public void CompressTwoFilesIntoOneZip() throws IOException
   {
      final String dummyFile0 = MyFolderUtil.uploadFolder.getAbsolutePathOfFile( "dummy.0.txt" );
      final String dummyFile1 = MyFolderUtil.uploadFolder.getAbsolutePathOfFile( "dummy.1.txt" );
      final String[] dummyFiles = new String[] { dummyFile0, dummyFile1 };

      writeDummyData( dummyFile0, 10 );
      writeDummyData( dummyFile1, 10 );

      final String zipPath = MyFolderUtil.backupFolder.getAbsolutePathOfFile( "testOneForTwo" );
      final String[] zipFilenames = MyFileCompressor.compressFiles( zipPath, parentFolderOfUpload,
            dummyFiles, 30,
            MyFileCompressor.CompressedFilenamePostfixType.THREE_DIGIT_START_FROM_ZERO );

      assertEquals( 1, zipFilenames.length );
   }

   @Test
   public void CompressTwoFilesIntoTwoZip() throws IOException
   {
      final String dummyFile0 = MyFolderUtil.uploadFolder.getAbsolutePathOfFile( "dummy.0.txt" );
      final String dummyFile1 = MyFolderUtil.uploadFolder.getAbsolutePathOfFile( "dummy.1.txt" );
      final String[] dummyFiles = new String[] { dummyFile0, dummyFile1 };

      writeDummyData( dummyFile0, 30 );
      writeDummyData( dummyFile1, 30 );

      final String zipPath = MyFolderUtil.backupFolder.getAbsolutePathOfFile( "testTwoForTwo" );
      final String[] zipFilenames = MyFileCompressor.compressFiles( zipPath, parentFolderOfUpload,
            dummyFiles, 30,
            MyFileCompressor.CompressedFilenamePostfixType.THREE_DIGIT_START_FROM_ZERO );

      assertEquals( 2, zipFilenames.length );
   }

   private void writeDummyData( String dummyFile, final int sizeofFile ) throws IOException
   {
      char dataSet[] = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };

      final FileOutputStream fh = new FileOutputStream( dummyFile );
      for( int i = 0; i < sizeofFile; ++i )
      {
         fh.write( dataSet[ i % dataSet.length ] );
      }
   }

   @Test
   public void getZipFilename()
   {
      final String zipFilename = MyFileCompressor.getZipFilename( "Base", 3,
            MyFileCompressor.CompressedFilenamePostfixType.THREE_DIGIT_START_FROM_ZERO );
      assertEquals( "Base.003.zip", zipFilename );
   }
}
